<?php


namespace App\Services;


use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

class ExpressService
{
    public function add($order)
    {
        $cityService = new cityService();
        $app = app('wechat.mini_program');
        $user = User::find($order->uid);
        $detail = $order->detail->where('type',0)->pluck("sku","goods_name");
        $detail_str="";
        foreach ($detail as $k=>$v){
            $detail_str.=$k." ".$v.';';
        }
        Log::info("商品信息:",[$detail_str]);
        $detais=[];
        foreach ($order->detail as $key=>$value)
        {
            $detais[$key]["name"]=$value["goods_name"];
            $detais[$key]["count"]=$value["number"];
        }
        if ($order->detail->where('type',0)->count()>0){
            $express_data = [
                "add_source" => 0,
                "order_id" => $order->order_number,
                "openid" => $user->openid,
                "delivery_id" => "SF",
                "biz_id" => "0210123137",
                "expect_time"=>Carbon::now()->addMinutes(30)->timestamp,
                "custom_remark"=>$detail_str,
                "sender" => [
                    "name" => "瑜伽啦呱",
                    "mobile" => 18501637396,
                    "company"=>"世焱信息科技(上海)有限公司",
                    "province" => "上海市",
                    "city" => "上海市",
                    "area" => "徐汇区",
                    "address" => "淮海中路999号环贸办公楼一期702"
                ],
                "receiver" => [
                    "name" => $order->address_consignee,
                    "mobile" => $order->address_phone_number,
                    "province" => $order->address_province,
                    "city" => $order->address_city,
                    "area" => $order->address_area ?? "市辖区",
                    "address" => $order->address_detail
                ],
                "cargo" => [
                    "count" => 1,
                    "weight" => 1,
                    "space_x" => 20,
                    "space_y" => 10,
                    "space_z" => 10,
                    "detail_list" => $detais
                ],
                "shop" => [
                    "wxa_path" => "pages/order/order",
                    "img_url" => "https://youga.one-cloud.xyz/images/upload/5f4db90b93869.jpeg",
                    "goods_name" => $detail_str,
                    "goods_count" => $order->detail->count()
                ],
                "insured" => [
                    "use_insured" => 0,
                    "insured_value" => 0
                ],
                "service" => [
                    "service_type" => 3,
                    "service_name" => "顺丰标快"
                ]

            ];
//         dd($express_data);
            $result = $app->express->createWaybill($express_data);
            Log::info("物流",$result);
            if ($result['errcode']==0) {
                Log::info("物流生成成功", [$result]);
                $order->tracking_no = $result['waybill_id'];
                $order->delivery_name = "顺丰快递";
                $order->delivery_id = "SF";
                $order->deliveryStatus = 1;
                $order->save();
                return $result['waybill_id'];
            }
        }else{
            $order->deliveryStatus = 1;
            $order->save();
            return 0;
        }


    }
}
